//@dart=2.12
import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/abnormal_event_center/view_model/abnormal_event_center_view_model.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/util/ext_utils.dart';

import '../../bad_event/ui/bad_event_list_page.dart';
import '../../res/gaps.dart';
import '../../widgets/common_widget.dart';

class AbnormalEventCenterPage extends BaseStatefulMvvmWidget{
  @override
  State<StatefulWidget> createState() => _AbnormalEventCenterState();

}

class _AbnormalEventCenterState extends BaseMvvmRefreshState<AbnormalEventCenterViewModel,AbnormalEventCenterPage>{

  GlobalKey<ScaffoldState> key = GlobalKey();

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      key: key,
      backgroundColor: Colours.bg_color,
      appBar: AppBar(title: Text('健康预警'),actions: [
        filterWidget().onTap(() {
          key.currentState?.openEndDrawer();
        })
      ],),
      body: loadingContentWidget,
      endDrawer: endDraw(),
    );
  }

  Widget endDraw() {
    return Column(
      children: [
        AppBar(
          leading: Container(),
          actions: [Container()],
          title: Text('筛选'),
        ),
        SingleChildScrollView(
          padding: EdgeInsets.symmetric(horizontal: 12),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              SizedBox(
                height: 16,
              ),
              CommonText('长者姓名'),
              SizedBox(
                height: 16,
              ),
              NoBorderInput(
                  hintText: '输入长者姓名',
                  textSize: 14,
                  onChanged: (str) {
                    viewModel.elderNameLike = str;
                  },
                  maxLines: 1,
                  textInputAction: TextInputAction.search,
                  onSubmitted: (str) {
                    viewModel.requestRefresh();
                  })
                  .addToContainer(
                  decoration: BoxDecoration(
                      color: Color(0xFFF7F7F7),
                      borderRadius: 6.borderRadius),
                  padding:
                  EdgeInsets.symmetric(horizontal: 16, vertical: 8)),


              SizedBox(
                height: 16,
              ),
              CommonText('床位号'),
              SizedBox(
                height: 16,
              ),
              NoBorderInput(
                  hintText: '输入床位号',
                  textSize: 14,
                  onChanged: (str) {
                    viewModel.bedNoLike = str;
                  },
                  maxLines: 1,
                  textInputAction: TextInputAction.search,
                  onSubmitted: (str) {
                    viewModel.requestRefresh();
                  })
                  .addToContainer(
                  decoration: BoxDecoration(
                      color: Color(0xFFF7F7F7),
                      borderRadius: 6.borderRadius),
                  padding:
                  EdgeInsets.symmetric(horizontal: 16, vertical: 8)),


              ...[
                filterContent(
                    title: '楼栋', value: viewModel.building?.pickName ?? "")
                    .onTap(() {
                  viewModel.chooseBuilding();
                }),

                filterContent(
                    title: '异常类型', value: viewModel.eventClass?.name ?? "")
                    .onTap(() {
                  viewModel.chooseEventClass();
                }),

                filterContent(
                    title: '状态', value: viewModel.status?.name ?? "")
                    .onTap(() {
                  viewModel.chooseStatus();
                }),
              ].insertWidget(Gaps.dividerLine),
              Gaps.dividerLine,
            ],
          ),
        ).setWeight(1),
        SizedBox(
          height: 10,
        ),
        Row(
          children: [
            SizedBox(
              width: 16,
            ),
            TextButton(
              onPressed: () {
                key.currentState?.closeEndDrawer();
                viewModel.resetClick();
              },
              child: CommonText('重置',
                  textColor: Color(0xff333333), fontWeight: FontWeight.w500),
              style: ButtonStyle(
                  shape: MaterialStateProperty.all(RoundedRectangleBorder(
                      borderRadius: 6.borderRadius,
                      side: BorderSide(color: Colours.gray_99)))),
            ).setWeight(1),
            SizedBox(
              width: 12,
            ),
            TextButton(
                onPressed: () {
                  key.currentState?.closeEndDrawer();
                  viewModel.requestRefresh();
                },
                child: CommonText('确定', textColor: Colors.white),
                style: ButtonStyle(
                    backgroundColor: MaterialStateProperty.all(Colors.blue),
                    shape: MaterialStateProperty.all(RoundedRectangleBorder(
                      borderRadius: 6.borderRadius,
                      side: BorderSide(color: Colors.blue),
                    )))).setWeight(1),
            SizedBox(
              width: 16,
            ),
          ],
        ),
        SizedBox(
          height: 12,
        )
      ],
    ).addToContainer(
        width: MediaQuery.of(context).size.width * 0.85,
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.horizontal(left: 12.radius)));
  }

  @override
  Widget createScrollWidget() {
    return ListView(
      children: [
        SizedBox(height: 12,),
        if(viewModel.topMap!=null)
          Wrap(
            alignment: WrapAlignment.center,
            verticalDirection: VerticalDirection.up,
            spacing: 10,
            children: viewModel.topMap!.entries.map((e) => Column(
              children: [
                CommonText(e.key,textColor: Colours.gray_99),
                SizedBox(height: 8,),
                CommonText(e.value.toString(),textSize: 22,textColor: Colors.blue,fontWeight: FontWeight.bold)
              ],
            ).addToContainer(width: context.screenSize.width/3-20,
            padding: EdgeInsets.symmetric(vertical: 10),),).toList(),
          ).addToContainer(color: Colors.white),
        SizedBox(height: 12,),

        ...viewModel.listItems.map((e) {
          return Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  Text.rich(TextSpan(
                    text: e.elderName,style: TextStyle(color: Colours.text_dark,fontWeight: FontWeight.bold,fontSize: 17),
                    children: [
                      TextSpan(
                        text: "  ${e.buildingNo}#${e.roomNo}#${e.bedNo}",
                        style: TextStyle(color: Colours.text_dark,fontSize: 14)
                      )
                    ]
                  ),).setWeight(1),
                  
                  CommonText(e.status==0?'预警中':'已关闭',textSize: 14,textColor: Colors.white).addToContainer(
                    decoration: BoxDecoration(
                      color: e.status==0?Colors.red:Colors.grey,
                      borderRadius: 4.borderRadius
                    ),
                    padding: EdgeInsets.symmetric(horizontal: 5,vertical: 2)
                  )
                ],
              ),
              
              CommonText('触发时间：${e.createdDate}',textSize: 15),
              CommonText('异常类型：${e.eventClassName}【${e.eventClassSubsetName}】',textSize: 15),
              CommonText('异常说明：${e.detail}',textSize: 15),
              if(!e.endTime.isNullOrEmpty())
                CommonText('关闭时间：${e.endTime}',textSize: 15),

              if(e.status==0)
              TextButton(onPressed: (){
                viewModel.closeEvent(e.id);
              }, child: CommonText('关闭',textSize: 14,textColor: Colours.gray_99),style: TextButton.styleFrom(
                shape: StadiumBorder(side: BorderSide(color: Colours.gray_99)),
                minimumSize: Size(double.infinity, 36)
              ),)
            ].insertWidget(SizedBox(height: 8,)),
          ).addToContainer(
            margin: EdgeInsets.symmetric(horizontal: 16),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: 5.borderRadius
            ),
            padding: EdgeInsets.symmetric(horizontal: 10,vertical: 12)
          );
        }).toList().insertWidget(SizedBox(height: 12,))
      ],
    );
  }

  @override
  AbnormalEventCenterViewModel createViewModel() {
    return AbnormalEventCenterViewModel(context);
  }

  @override
  bool get canPullUp => true;
  
}

